package it.codelair.bakekaservice;

import it.codelair.bakekaservice.Utilities.EntiManager;
import it.codelair.bakekaservice.contentProvider.EntiProvider;
import android.os.Bundle;
import android.app.Activity;
import android.content.Context;
import android.database.Cursor;
import android.support.v4.app.FragmentActivity;
import android.support.v4.app.LoaderManager.LoaderCallbacks;
import android.support.v4.content.CursorLoader;
import android.support.v4.content.Loader;
import android.support.v4.widget.SimpleCursorAdapter;
import android.view.Menu;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.Toast;

public class ServicesActivity extends FragmentActivity implements LoaderCallbacks<Cursor>{
	Context mContext = this;
	private static final int LOADER_ID = 1;
	private SimpleCursorAdapter dataSource;
	private String category;
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_services);
		Bundle b = getIntent().getExtras();
		this.setCategory(b.getString("category"));
		getSupportLoaderManager().initLoader(LOADER_ID, null, this); //id, bundle loadercallback
        //null come cursor
		Cursor cursor=mContext.getContentResolver().query(EntiProvider.ENTI_CONTENT_URI, null,"category = ?", new String[]{category}, "_id");
        dataSource = new SimpleCursorAdapter(getApplicationContext(),R.layout.activity_services,
        		cursor,new String[]{"_id"},new int[]{R.id.number_entry},SimpleCursorAdapter.FLAG_REGISTER_CONTENT_OBSERVER);
        
        ListView myList = (ListView)findViewById(R.layout.list);
        myList.setAdapter(dataSource);
		
		
//		Cursor cursor=mContext.getContentResolver().query(EntiProvider.ENTI_CONTENT_URI, null,"category = ?", new String[]{category}, "business_name");
//		LinearLayout l = (LinearLayout)findViewById(R.id.serviceLayout);
//		if(cursor!=null){
//			int nameId =cursor.getColumnIndex("business_name");
//			while(cursor.moveToNext()){
//				String name=cursor.getString(nameId);
//				TextView tex = new TextView(mContext);
//				tex.setText(name);
//				l.addView(tex);
//			}
//			cursor.close();
//		}
//		Toast t = Toast.makeText(mContext, "Categoria: "+category, Toast.LENGTH_LONG);
//		t.show();
	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		// Inflate the menu; this adds items to the action bar if it is present.
		getMenuInflater().inflate(R.menu.services, menu);
		return true;
	}
	
	@Override
	public Loader<Cursor> onCreateLoader(int arg0, Bundle arg1) {
		CursorLoader cl = new CursorLoader(getApplicationContext(), EntiProvider.ENTI_CONTENT_URI,
				new String[]{"_id"},null,null,null);
		return cl;
	}

	@Override
	public void onLoadFinished(Loader<Cursor> arg0, Cursor c) {
		dataSource.swapCursor(c);
		
	}

	@Override
	public void onLoaderReset(Loader<Cursor> arg0) {
		dataSource.swapCursor(null);
	}

	@Override
    protected void onDestroy() {
    	super.onDestroy();
    	getSupportLoaderManager().destroyLoader(LOADER_ID);
    }

	public SimpleCursorAdapter getDataSource() {
		return dataSource;
	}

	public void setDataSource(SimpleCursorAdapter dataSource) {
		this.dataSource = dataSource;
	}

	public String getCategory() {
		return category;
	}

	public void setCategory(String category) {
		this.category = category;
	}

}
